package com.banmao.algorithm.No501To700.No700;

public class Solution1 {

    public TreeNode searchBST(TreeNode root, int val) {

        TreeNode subRoot = recursive(root, val);

        return subRoot;
    }

    private TreeNode recursive(TreeNode root, int val) {

        if(root == null) {
            return null;
        }

        // 判断val值与root.val的大小关系
        if(root.val == val) {
            return root;
        }

        if(root.val < val) {
            return recursive(root.right, val);
        } else {
            return recursive(root.left, val);
        }

    }

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
